import { NextRequest, NextResponse } from 'next/server';
import UserModel, { WorkExperience } from '@/models/user';
import { authMiddleware, TokenPayload } from '@/utils/auth';

interface RouteParams {
  params: {
    id: string;
  };
}

// 更新工作经历
export async function PUT(request: NextRequest, { params }: RouteParams) {
  try {
    const { id } = params;
    const workExpId = parseInt(id, 10);
    
    if (isNaN(workExpId)) {
      return NextResponse.json(
        { error: '无效的工作经历ID' },
        { status: 400 }
      );
    }
    
    // 验证用户身份
    const authResult = await authMiddleware(request);
    if (authResult instanceof NextResponse) {
      return authResult; // 返回错误响应
    }
    
    const decoded = authResult as TokenPayload;
    const userId = decoded.id;
    
    // 获取请求数据
    const body = await request.json();
    const workExpData = body as Partial<WorkExperience>;
    
    // 验证至少有一个字段需要更新
    if (Object.keys(workExpData).length === 0) {
      return NextResponse.json(
        { error: '没有提供要更新的字段' },
        { status: 400 }
      );
    }
    
    // 确保只能更新自己的工作经历
    const workExperiences = await UserModel.getWorkExperiences(userId);
    const workExp = workExperiences.find(exp => exp.id === workExpId);
    
    if (!workExp) {
      return NextResponse.json(
        { error: '工作经历不存在或无权限修改' },
        { status: 404 }
      );
    }
    
    // 更新工作经历
    const success = await UserModel.updateWorkExperience(workExpId, workExpData);
    
    if (!success) {
      return NextResponse.json(
        { error: '更新工作经历失败' },
        { status: 400 }
      );
    }
    
    // 获取更新后的工作经历列表
    const updatedWorkExperiences = await UserModel.getWorkExperiences(userId);
    
    return NextResponse.json(
      { 
        message: '更新工作经历成功',
        workExperiences: updatedWorkExperiences
      },
      { status: 200 }
    );
    
  } catch (error) {
    console.error('更新工作经历错误:', error);
    return NextResponse.json(
      { error: '服务器错误，请稍后再试' },
      { status: 500 }
    );
  }
}

// 删除工作经历
export async function DELETE(request: NextRequest, { params }: RouteParams) {
  try {
    const { id } = params;
    const workExpId = parseInt(id, 10);
    
    if (isNaN(workExpId)) {
      return NextResponse.json(
        { error: '无效的工作经历ID' },
        { status: 400 }
      );
    }
    
    // 验证用户身份
    const authResult = await authMiddleware(request);
    if (authResult instanceof NextResponse) {
      return authResult; // 返回错误响应
    }
    
    const decoded = authResult as TokenPayload;
    const userId = decoded.id;
    
    // 确保只能删除自己的工作经历
    const workExperiences = await UserModel.getWorkExperiences(userId);
    const workExp = workExperiences.find(exp => exp.id === workExpId);
    
    if (!workExp) {
      return NextResponse.json(
        { error: '工作经历不存在或无权限删除' },
        { status: 404 }
      );
    }
    
    // 删除工作经历
    const success = await UserModel.deleteWorkExperience(workExpId);
    
    if (!success) {
      return NextResponse.json(
        { error: '删除工作经历失败' },
        { status: 400 }
      );
    }
    
    // 获取删除后的工作经历列表
    const updatedWorkExperiences = await UserModel.getWorkExperiences(userId);
    
    return NextResponse.json(
      { 
        message: '删除工作经历成功',
        workExperiences: updatedWorkExperiences
      },
      { status: 200 }
    );
    
  } catch (error) {
    console.error('删除工作经历错误:', error);
    return NextResponse.json(
      { error: '服务器错误，请稍后再试' },
      { status: 500 }
    );
  }
} 